<?php

	Zend_Loader::loadClass('Zend_Controller_Action');
	Zend_Loader::loadClass('Zend_Filter_Alnum');
	
	class IndexController extends Zend_Controller_Action{
	
	
		private $ProdsPerPage = 18; //liczba produkt�w na stronie
		private $StartPages = 0;  //pocz�tkowa strona w menu stronicowania
		private $MaxPages = 10; //maksymalna liczba stron w menu stronicowania
		
		private $Subcategories = array(); //lista podkategorii danej kategorii

		private $Products = array();  //lista produkt�w z kategorii (i jej podkategorii)
		
		private $Categories = array();
		
		private $exturl = 'http://moulin.com.pl/';
		private $siteurl = 'http://ideowski.nazwa.pl/zend/';
	
		public function indexAction(){
			
			$db = Globals::getDBConnection();
			
			
			$translate = new Zend_Translate('gettext', Globals::getConfig()->dirs->app.'/languages/'.Globals::getLocale().'.mo', Globals::getLocale());
			$translate->setLocale(Globals::getLocale());
			
			$this->view->translate = $translate;
			
			$StripZnaki = new Zend_Filter_Alnum();
			
			$tmpsql = 'select id, kategoria_pl as nazwa from moulin__oferta where stan=1 and prev_id=0 order by kolej asc';
			$kategorie = $db->fetchAll($tmpsql);
			
			
			
			$this->view->assign('exturl', Globals::getConfig()->exturl);
			$this->view->assign('siteurl', Globals::getConfig()->siteurl);
			
			$this->view->assign('kategorie', $kategorie);
			
			if($kategorie){
				$request = $this->getRequest();
				$kat_id = $request->getParam('kategoria');
			
				$kat_id = ($kat_id==''?$kategorie[0]['id']:$kat_id);
				
				
				
				$this->view->assign('nazwa', $StripZnaki->filter($kategorie[0]['nazwa']));
				
				/*
				 * lista produkt�w z kategorii ze stronicowaniem
				 */
				 $strona = $request->getParam('strona');
				 
				 $ile = $db->fetchRow('select count(*) as ile from moulin__oferta where id='.$kat_id.' and stan=1');
				 
				
				 $ile = $ile['ile'];
				 
				
				 
				if($ile==1 || $kat_id==0){
				
					
					$this->SelectProducts($kat_id);
					
					 
					$start = $this->ProdsPerPage*$strona;
					$kaunt = count($this->Products);
					$stron = ceil($kaunt/$this->ProdsPerPage);
					if($strona+1>$this->MaxPages) $this->StartPages=$strona;
					
					$produkty = array_slice($this->Products, $start, $this->ProdsPerPage);
					
					$this->view->assign('produkty', $produkty);
					
					$this->view->assign('stron', $stron);
					$this->view->assign('start_pages', $this->StartPages);
					$this->view->assign('max_pages', $this->MaxPages);
					$this->view->assign('strona', $strona);
					$this->view->assign('start', $start+1);
					$this->view->assign('stop', $start+1+$this->ProdsPerPage);
					$this->view->assign('kaunt', $kaunt);
					
					
					$this->view->assign('kat_id', $kat_id);
				}else{
					$this->view->assign('inactive', 1);
				}
			}
			
			
		}
		
		public function helperAction(){
			$db = Globals::getDBConnection();
			
			$StripZnaki = new Zend_Filter_Alnum();
			
			$this->view->assign('exturl', Globals::getConfig()->exturl);
			$this->view->assign('siteurl', Globals::getConfig()->siteurl);
		
			$opid = $_POST['productId']; //$this->getParam('opid');
		
			$sql = 'select op.id, op.nazwa_pl as nazwa, op.podnazwa_pl as podnazwa, op.cena, op.cena_promocja, op.czy_nowosc, op.czy_glowna, p.nazwa_pl as producent, op.rabat from moulin__oferta_produkty op, moulin__producenci p  where op.id_producenta=p.id and op.stan=1 and op.czy_FB=1 and op.id=\''.$opid.'\' order by op.kolej asc limit 1';
		
		
			$praca = $db->fetchAll($sql);
			
			
			
			
				if(is_array($praca)){
					$praca = $praca[0];
						$sql = 'select nazwa_pl as nazwa, zdjecie, thumb from moulin__oferta_produkty_foto where stan=1 and id_oferta_produkty='.$praca['id'].' order by kolej asc limit 1';
					
						$foto = $db->fetchAll($sql);
						
						$praca['url'] = $StripZnaki->filter($praca['nazwa']);
						
						$praca['foto'] = $foto;
						
						if($praca['rabat']!=''){
							$rabatArr = explode("\r\n", $praca['rabat']);
							if(is_array($rabatArr)){
								$rabat = explode(";", $rabatArr[count($rabatArr)-1]);
								
								$praca['rabatArr'] = $rabat;
							}
						}
					
					$this->view->assign('produkt', $praca);
				}
			
		}
		
		
		 private function SelectSubcategories($id){
			$db = Globals::getDBConnection();
			
			$sql = 'select id from moulin__oferta where stan=1 and prev_id='.$id;
			
			$sub = $db->fetchAll($sql);
			if(is_array($sub)){
				foreach($sub as $s){
					$this->Subcategories[count($this->Subcategories)] = $s['id'];
					$this->SelectSubcategories($s['id']);
				}
			}
		 }
		  private function SelectProducts($kat_id){
		  
			$db = Globals::getDBConnection();
 
 
			$this->Subcategories = array($kat_id);
			$this->SelectSubcategories($kat_id);
			
			if(is_array($this->Subcategories)){
				foreach($this->Subcategories as $sub){
					$sql = 'select op.id, op.nazwa_pl as nazwa, op.podnazwa_pl as podnazwa, op.cena, op.cena_promocja, op.czy_nowosc, op.czy_glowna, p.nazwa_pl as producent, op.rabat from moulin__oferta_produkty op, moulin__producenci p  where op.id_producenta=p.id and op.stan=1 and op.czy_FB=1 and op.id_oferta=\''.$sub.'\' order by op.kolej asc';
				
					$produkty = $db->fetchAll($sql);
						
					if(is_array($produkty)){
						foreach($produkty as $key=>$value){
							$sql = 'select nazwa_pl as nazwa, zdjecie, thumb from moulin__oferta_produkty_foto where stan=1 and id_oferta_produkty='.$value['id'].' order by kolej asc limit 1';
							
							$foto = $db->fetchAll($sql);
							$produkty[$key]['foto_nazwa'] = $foto[0]['nazwa'];
							$produkty[$key]['zdjecie'] = $foto[0]['thumb'];
							
							
							
							if($value['rabat']!=''){
								$rabatArr = explode("\r\n", $value['rabat']);
								if(is_array($rabatArr)){
									$rabat = explode(";", $rabatArr[count($rabatArr)-1]);
									
									$produkty[$key]['rabatArr'] = $rabat;
								}
							}
							
							$this->Products[count($this->Products)] = $produkty[$key];
						}
					}
				}
			}
		 }
	
	}

?>